package com.ivan.stu.notetool.utils;

import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Author: gongwq on 2017/8/7 0007 .
 * Email: gwq_0111@163.com
 * Descriptions: 获取当前日期的工具类
 */

public class DateUtils {
    /**
     * 获取当前时间  格式2017-08-01 12:26:43
     *
     * @return
     */
    public static String getNowDate() {
        Calendar now = Calendar.getInstance();
//        System.out.println("年: " + now.get(Calendar.YEAR));
//        System.out.println("月: " + (now.get(Calendar.MONTH) + 1) + "");
//        System.out.println("日: " + now.get(Calendar.DAY_OF_MONTH));
//        System.out.println("时: " + now.get(Calendar.HOUR_OF_DAY));
//        System.out.println("分: " + now.get(Calendar.MINUTE));
//        System.out.println("秒: " + now.get(Calendar.SECOND));
//        System.out.println("当前时间毫秒数：" + now.getTimeInMillis());
//        System.out.println(now.getTime());

        Date d = new Date();
        // System.out.println(d);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        //  System.out.println("格式化后的日期：" + dateNowStr);

//        String str = "2012-1-13 17:26:33";  //要跟上面sdf定义的格式一样
//        Date today = null;
//        try {
//            today = sdf.parse(str);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        System.out.println("字符串转成日期：" + today);
        return dateNowStr;
    }

    public static String dateToStamp(String time) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(time);
        long ts = date.getTime();
        return String.valueOf(ts);
    }


    /**
     * 获取指定日期的时间，返回格式为2017-08-07
     *
     * @param count <br/>
     *              -1 表示昨天的日期<br/>
     *              0  表示今天的日期<br/>
     *              1 表示明天的日期
     * @return
     */
    public static String getCustomDate(int count) {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, count);//把日期往后增加一天.整数往后推,负数往前移动
        date = calendar.getTime(); //这个时间就是日期往后推一天的结果
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 获取这周周一是几号
     *
     * @return
     */
    public static String getMondayOFWeek() {
        Date date=new Date();
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek()); // Monday
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        return sdf.format(c.getTime());
      }

    // 获得当前日期与本周日相差的天数
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK) - 1; // 因为按中国礼拜一作为第一天所以这里减1
        if (dayOfWeek == 1) {
            return 0;
        } else {
            return 1 - dayOfWeek;
        }
    }

    /**
     * 获取这个月的第一天
     *
     * @return
     */
    public static String getFirstDayOfMonth() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1 号
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 计算当月最后一天,返回 2017-08-31
     */

    public static String getLastDayOfMonth() {
        String str = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastDate = Calendar.getInstance();
        lastDate.set(Calendar.DATE, 1);// 设为当前月的1 号
        lastDate.add(Calendar.MONTH, 1);// 加一个月，变为下月的1 号
        lastDate.add(Calendar.DATE, -1);// 减去一天，变为当月最后一天
        str = sdf.format(lastDate.getTime());
        return str;
    }

    /**
     * 获得本周星期日的日期  2017-08-31
     */
    public static String getSundayOfWeek() {
        Date date = new Date();
        Calendar c = new GregorianCalendar();
        c.setFirstDayOfWeek(Calendar.MONDAY);
        c.setTime(date);
        c.set(Calendar.DAY_OF_WEEK, c.getFirstDayOfWeek() + 6); // Sunday
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(c.getTime());

    }

    public static void getMondayAndSunday(String time) {
        Calendar cal = Calendar.getInstance();

        try {

            cal.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time));

        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        //所在周开始日期
        Log.e("Monday--->",new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
       // Constants.MONDAY_OF_THIS_WEEK = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
        cal.add(Calendar.DAY_OF_WEEK, 6);
        //所在周结束日期
        Log.e("Sunday--->",new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime()));
      //  Constants.SUNDAY_OF_THIS_WEEK = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
    }



    /**
     * 获取开始到结束之间的时间差值 (秒)
     *
     * @param start yyyy-MM-dd HH:mm:ss
     * @param end yyyy-MM-dd HH:mm:ss
     * @return
     * @throws ParseException
     */
    public static String getStart2EndTime(String start, String end) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
//        String date1 = "2014-10-10 00:00:00";
//        String date2 = "2015-10-10 00:00:00";
        c.setTime(sdf.parse(start));
//        int y1 = c.get(Calendar.YEAR);
//        int m1 = c.get(Calendar.MONTH);

        c.setTime(sdf.parse(end));
//        int y2 = c.get(Calendar.YEAR);
//        int m2 = c.get(Calendar.MONTH);
//
//        int y = Math.abs(y2 - y1);
//        int m = y * 12 + Math.abs(m1-m2);

        long d1 = sdf.parse(start).getTime();
        long d2 = sdf.parse(end).getTime();
        int offset = (int) (Math.abs(d2 - d1) / (1000));
        return offset + "";
    }

    /**
     * 比较两个时间的大小 yyyy-MM-dd
     */
    public static boolean compareTime(String firstTime,String secondTime){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        try{
            c1.setTime(df.parse(firstTime));
            c2.setTime(df.parse(secondTime));
            int result = c1.compareTo(c2);
            if (result < 0)
                return false;
            else
                return true;
        }catch (Exception e){
            return true;
        }

    }
}
